package com.zuoshen_algorithm;

/**
 * 项目名称: java_algorithm
 * @ClassName SpinningPrinting （有限几个变量实现正方形矩阵旋转90度）
 * @Description
 * @Author huipanxing huipanxing@chinasofti.com   @Date 2022/2/28 16:05
 */
public class SpinningPrinting {

    public static void main(String[] args) {
        int[][] array = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
        System.out.println("旋转之前打印：");
        Util.printArray(array);
        int a = 0;
        int b = 0;
        int c = array.length - 1;
        int d = array[0].length - 1;
        while (a <= c && b <= d) {
            spinning(array, a++, b++, c--, d--);
        }
        System.out.println("旋转之后打印：");
        Util.printArray(array);
    }

    public static void spinning(int[][] array, int a, int b, int c, int d) {
        int size = c - a;
        for (int i = 0; i < size; i++) {
            int temp=array[a][b+i];
            array[a][b+i]=array[c-i][b];
            array[c-i][b]=array[c][d-i];
            array[c][d-i]=array[a+i][d];
            array[a+i][d]=temp;
        }
    }

}
